package com.zhdl.network.service.impl;

import com.project.command.instruction.bo.SingleInstruction;
import com.zhdl.common.util.IdUtil;
import com.zhdl.instruction.ExecuteInstructionService;
import com.zhdl.network.service.UpsService;
import com.zhdl.network.service.conf.PortConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;
import static com.zhdl.network.prefirertu.after.UpsAfterService.UPS_AFTER_SERVICE;
import static com.zhdl.network.prefirertu.analysis.UpsAnalysis.UPS_ANALYSIS;

@Slf4j
@Service
public class UpsServiceImpl implements UpsService {

    @Resource
    private ExecuteInstructionService instructionService;

    @Resource
    private PortConf portConf;

    @Override
    public void send(String cmd) {
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());
        //设置要发送的指令字符串
        instruction.setInstruction(cmd);
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(UPS_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
        instruction.setPortKey(portConf.getComPortUps());
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(UPS_ANALYSIS);
        instructionService.perform(instruction);
    }

    @Override
    public void sendQ1() {
        send("51 31 0D");
    }

    /**
     * 发送Q6指令 读取指令
     */
    @Override
    public void sendQ6() {
        send("51 36 0D");
    }

}
